package com.aptg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class PromotionRelation {
	
	public static void main(String[] args) throws IOException, SQLException {
		
		String driver = "com.mysql.jdbc.Driver"; 
        String url = "jdbc:mysql://localhost:3306/eshop?useUnicode=true&characterEncoding=utf8"; 
        String user = "es_adm"; 
        String password = "es_adm"; 
        
        Connection conn = null;
        try { 
            Class.forName(driver); 
            conn = DriverManager.getConnection(url,  user, password);
            if(conn != null && !conn.isClosed()) {
                //System.out.println("資料庫連線測試成功！"); 
            }
            
        } 
        catch(ClassNotFoundException e) { 
            System.out.println("找不到驅動程式類別"); 
            e.printStackTrace(); 
        } 
        catch(SQLException e) { 
            e.printStackTrace(); 
        } 
		
		try {
			String sql = null;
			
			File file = new File("E://eshop_oes_data//promo_relation.txt");
			BufferedReader brdFile = new BufferedReader(new FileReader(file));
			String strLine = null;

			// File outFile = new File("E://eshop_oes_data//promo_relation.sql");
			// BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
			
			Statement stmt = conn.createStatement();
			int count = 0;
			while ((strLine = brdFile.readLine()) != null) {
				String[] data = strLine.split(",");
				
				sql = "";
				sql += "insert into apt_promo_relation (promo_code, gift_code, tm_code, sp_code, break_mon1, break_amount1, break_mon2, break_amount2, break_mon3, break_amount3, gift_detail_data, remark, bouns_point, deposit, break_amount1_new, break_amount2_new, break_amount3_new, divided_date, tm_apply_str_date, tm_apply_end_date, update_time)";
				sql += " values ('" + data[0] + "','" + data[1] + "','" + data[2] + "','" + data[3] + "','" + data[4] + "','" + data[5] + "','" + data[6] + "','" + data[7] + "','" + data[8] + "','" + data[9] + "','" + data[10] + "','" + data[11] + "','" + data[12] + "','" + data[13] + "','" + data[14] + "','" + data[15] + "','" + data[16] + "','" + data[17] + "','" + data[18] + "','" + data[19] + "','2011-01-18 03:23:06')";
				sql += ";";
				
				conn.setAutoCommit(false);
				stmt.addBatch(sql);
				
				count++;
				
				if(count % 5000 == 0) {
					stmt.executeBatch();
					conn.commit();
					
					stmt.clearBatch();
				}
			}
			
			stmt.executeBatch();
			conn.commit();
			
			stmt.clearBatch();
			
			conn.close();
			brdFile.close();
			// writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
